package com.zhangtai.modules.service.impl.system;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangtai.modules.dao.OrgGroupDao;
import com.zhangtai.modules.dto.GroupEntityDto;
import com.zhangtai.modules.entity.system.OrgGroupEntity;
import com.zhangtai.modules.service.system.OrgGroupService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class OrgGroupServiceImpl extends ServiceImpl<OrgGroupDao, OrgGroupEntity> implements OrgGroupService {

    @Autowired
    private OrgGroupDao orgGroupDao;
    @Override
    public PageResult queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<OrgGroupEntity> page = this.page(
                new Page<OrgGroupEntity>(pageNum,pageSize),
                new QueryWrapper<OrgGroupEntity>().like(!StringUtils.isEmpty(keyword),"name",keyword)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }


    @Override
    public PageResult queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "groupName", required = false) String groupName,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        Page<GroupEntityDto> page = new Page<GroupEntityDto>(pageNum,pageSize);
        List<GroupEntityDto> list = orgGroupDao.getOrgGroupPage(groupName,page);
        page.setRecords(list);
        return new PageResult(page);
    }

//    @Override
//    public List<Long> getOrgList(@RequestParam(value="roleId")List<Long> roleId){
//        return roleOrgDao.getRoleOrgList(roleId);
//    }

    @Override
    public OrgGroupEntity getByIdE(Long id) {
            OrgGroupEntity one = this.getOne(new QueryWrapper<OrgGroupEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(OrgGroupEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(OrgGroupEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            OrgGroupEntity entity = new OrgGroupEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}